package top.healthylife.service.impl;/*
 *@Author GaoZeXi
 *@Created time 2020/4/7 1:17
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.healthylife.domain.CheckGroup;
import top.healthylife.entity.PageResult;
import top.healthylife.entity.QueryPageBean;
import top.healthylife.mapper.CheckGroupMapper;
import top.healthylife.service.CheckGroupService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        int currentPage = queryPageBean.getCurrentPage();
        int pageSize =  queryPageBean.getPageSize();
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        String queryString  = queryPageBean.getQueryString();
        Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryString);
        System.out.println("findPage======++++++++++===========");
        System.out.println("总记录=============="+page.getTotal());
        System.out.println("结果集=============="+page.getResult());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        //清除该检查组原有的检查项
        checkGroupMapper.deleteAssociation(checkGroup.getId());
        //建立新的联系
        setCheckGroupAndCheckItem(checkGroup.getId(),checkItemIds);
        //更新检查组信息
        checkGroupMapper.edit(checkGroup);
    }

    @Override
    public void delete(Integer id) {
        //先清除该检查组原有的检查项
        checkGroupMapper.deleteAssociation(id);
        //删除检查组
        checkGroupMapper.delete(id);

    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] itemIds) {
        //先添加到t_checkgroup表中,然后拿到自动生成的id ,将id,itemIds添加到关联表中
            checkGroupMapper.add(checkGroup);
            Integer id = checkGroup.getId();

            setCheckGroupAndCheckItem(id,itemIds);
    }


    private void setCheckGroupAndCheckItem(Integer id, Integer[] checkItemIds) {
        if(checkItemIds !=null && checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",id);
                map.put("checkitem_id",checkItemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
    @Override
    public List<CheckGroup> findAll(){
        return checkGroupMapper.findAll();
    }


}
